<template>
  <view class="login" v-if="show">
    <view :style="{height: `${getSystemInfo.navBarHeight}px`}">
      <navagation-bar>
        <template>
          <view
            :style="{
              width: `calc(100vw - ${getSystemInfo.menuRight * 2}px)`,
              textAlign: 'center'
            }"
          >
            <text>阿特拉斯卡友之家</text>
          </view>
        </template>
      </navagation-bar>
    </view>
    
    <view class="logo">
      <u--image
        mode="widthFix"
        src="/static/img/logo1@2x.png"
        width="calc(100vw - 130rpx)"
        height="auto">
      </u--image>
    </view>
    
    <view class="agreement">
      <view class="pt2">
        <u-checkbox-group size="18px" icon-size="14px" shape="circle" v-model="agree">
          <u-checkbox name="agree"></u-checkbox>
        </u-checkbox-group>
      </view>
      <view>
        我已阅读并同意
        <text class="a-name" @click="onKyzjServiceProtocol">《卡友之家服务协议》</text>
        和<text class="a-name" @click="onKyzjPrivacyPolicy">《卡友之家隐私政策》</text>
      </view>
    </view>
    
    <view class="login-types">
      <u-button
        type="primary"
        text="微信登录"
        icon="weixin-fill"
        :custom-style="{ height: '88rpx' }"
        :disabled="agree[0] !== 'agree'"
        :open-type="agree[0] === 'agree' && 'getPhoneNumber'"
        @getphonenumber="onWxLogin">
      </u-button>
      <u-button
        text="手机号快捷登录"
        :custom-style="{ height: '88rpx' }"
        @click="toPhoneLogin">
      </u-button>
    </view>
    
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
  import loginMixin from '@/mixins/login'
  import { mapGetters, mapActions } from 'vuex'
  import navagationBar from '@/components/navigationBar.vue'
  
  export default {
    mixins: [loginMixin],
    components: {
    	navagationBar
    },
    data() {
      return {
        // 是否同意
        agree: [],
		show:false
      }
    },
    computed: {
      ...mapGetters('global', ['getSystemInfo'])
    },
    methods: {
      ...mapActions('user', ['clearUserInfo']),
	  //查看是否需要三要素登录
	
	  async getLoginType(){
	  	const res = await this.$request({
	  	  url: '/user-center/loginPageConfig/getType',
	  	  method: 'GET',
	  	  data: {
	  	    client:4
	  	  }
	  	})
		//判断登录验证方式1.三要素登录，2.二要素登录
		if(res.datas.type == 1){
			this.show = false
			this.toPhoneLogin()
		}else{
			this.show = true
		}
		console.log(res)
	  },
      
      // 微信登录
      async onWxLogin(data) {
        const { code } = data.detail
				const params = { code }
				
				// 添加第三方渠道登录信息
				if(uni.getStorageSync('AppLaunchQuery')){
					const AppLaunchQuery = uni.getStorageSync('AppLaunchQuery')
					params.wxMaChannel = AppLaunchQuery.channel
				}
        if (code) {
          try {
            await this.loginForWexin(params)
            // await this.loginForWexin({ code })
            await this.getUserInfoFn()
            this.toIndex()
            uni.$emit('refreshIndex','login')
          } catch (err) {
            this.$refs.uToast.show({
            message: err.respMsg || '登录失败',
            position: 'top'
          })
         } 
        }
      },
	  
      
      // 校验微信用户是否注册
      // 如果注册，通过Promise返回openId
      async isRegister(wxCode) {
        try {
          const { datas } = await this.$request({
            url: '/api-user/app/users/login/valid/wechatRegister',
            method: 'GET',
            data: {
              code: wxCode,
              channel: 'MINIAPP'
            }
          })
          
          if (datas.registerFlag) {
            return Promise.resolve(datas.openid)
          }
          
          // 微信用户未在系统注册
          this.$refs.uToast.show({
            message: '用户未注册',
            position: 'top'
          })
          return Promise.reject()
        } catch (err) {
          return Promise.reject(err)
        }
      },
      
      // 获取微信code
      async getWxCode() {
        return new Promise((resolve, reject) => {
          uni.login({
          	provider: 'weixin',
          	success: (res) => resolve(res.code),
            fail: (err) => {
              this.$refs.uToast.show({
                message: '授权失败',
                position: 'top'
              })
              reject(err)
            }
          })
        })
      },
    
      // 分享
      onShareAppMessage(){
        return {
          title: '阿特拉斯卡友之家',
        }
      },
      onShareTimeline() {
        return {
          title: '阿特拉斯卡友之家',
        }
      },
		},
    onShow() {
	  this.getLoginType()
      this.clearUserInfo()
	  
    }
  }
</script>

<style scoped lang="scss">
  .login {
    padding: 0 65rpx;
    height: 100vh;
    background: #fff;
    display: flex;
    box-sizing: border-box;
    flex-direction: column;
    .logo {
      margin: 190rpx 0 205rpx;
    }
    .agreement {
      display: flex;
      flex-direction: row;
      font-size: 14px;
      line-height: 1.5;
      margin-bottom: 60rpx;
      .a-name {
        color: $uni-primary;
      }
    }
    .login-types {
      display: flex;
      flex-direction: column;
      gap: 48rpx;
      /deep/ .uicon-weixin-fill {
        font-size: 30px !important;
        line-height: 1 !important;
      }
    }
  }
</style>
